package de.uni.bremen.estudent.lbs.util;


import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.View;
import android.widget.TextView;

import de.uni.bremen.estudent.lbs.R;
import de.uni.bremen.estudent.lbs.provider.EstudentLBSContract.Meals;


/**
 * CursorAdapter for the mensa-table.
 * 
 * @author bahlfeld
 * @version 1.0 31.01.2012
 */
public final class MensaCursorAdapter extends SimpleCursorAdapter {

    /**
     * See ViewHolder pattern.
     * 
     * @author bahlfeld
     * @version 1.0 31.01.2012
     */
    private class ViewHolder {
        int mDescHolder;
        TextView mDescription;
        TextView mName;
        int mNameHolder;
        TextView mPrice;
        int mPriceHolder;
    }

    public MensaCursorAdapter(Context context, int layout, Cursor c,
                              String[] from, int[] to, int flags) {
        super(context, layout, c, from, to, flags);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void bindView(final View view, final Context context,
                         final Cursor cursor) {
        super.bindView(view, context, cursor);
        ViewHolder vh = (ViewHolder) view.getTag();
        if (vh == null) {
            vh = new ViewHolder();
            vh.mName = (TextView) view.findViewById(R.id.mealName);
            vh.mDescription = (TextView) view.findViewById(R.id.mealDesc);
            vh.mPrice = (TextView) view.findViewById(R.id.mealPrice);
            vh.mNameHolder = cursor.getColumnIndex(Meals.MEAL_NAME);
            vh.mDescHolder = cursor.getColumnIndexOrThrow(Meals.MEAL_DESC);
            vh.mPriceHolder = cursor.getColumnIndexOrThrow(Meals.MEAL_STUDENT_PRICE);
        }
        vh.mName.setText(cursor.getString(vh.mNameHolder));
        vh.mDescription.setText(cursor.getString(vh.mDescHolder));
        vh.mPrice.setText(cursor.getString(vh.mPriceHolder));
    }
}
